package com.wish.g04.renting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g04.base.entity.Dept;
import com.wish.g04.base.entity.HouseInfo;
import com.wish.g04.base.entity.PremisesInfo;
import com.wish.g04.base.service.IDeptService;
import com.wish.g04.base.service.IHouseInfoService;
import com.wish.g04.base.service.IPremisesInfoService;
import com.wish.g04.common.core.web.controller.BaseController;
import com.wish.g04.common.core.web.page.TableDataInfo;
import com.wish.g04.renting.dto.HouseInfoDTO;
import com.wish.g04.renting.service.ITbHouseInfoService;
import com.wish.g04.renting.vo.HouseInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 这个类的作者是123
 * 开发的日期是：2022/05/30
 **/
@Service
public class ITbHouseInfoServiceimpl extends BaseController implements ITbHouseInfoService {
   @Autowired
   IHouseInfoService iHouseInfoService;
   @Autowired
   IPremisesInfoService iPremisesInfoService;
   @Autowired
   IDeptService iDeptService;

    @Override
    public TableDataInfo selectTbHouseInfoList(HouseInfoDTO houseInfoDTO) {
        QueryWrapper<HouseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flg",0);
        //小区名称
        //租房类型
        if(StringUtils.hasText(houseInfoDTO.getRentType())){
            //公寓
            if(houseInfoDTO.getRentType().equals("1")){
                wrapper.eq("type",1);
            }
            //合租
            if(houseInfoDTO.getRentType().equals("3")){
                wrapper.eq("reserve1",1);
            }
            //整租
            wrapper.eq("reserve1",0);
        }
        List<HouseInfo> list = iHouseInfoService.list(wrapper);
        List<HouseInfoVO> houseInfoVOList = new ArrayList<>();
        list.forEach(houseInfo -> {
            HouseInfoVO houseInfoVO = new HouseInfoVO();
            BeanUtil.copyProperties(houseInfo,houseInfoVO);
            houseInfoVOList.add(houseInfoVO);
        });
        //获取所有楼盘id
        List<Integer> premisesIds = houseInfoVOList.stream().map(houseInfoVO -> houseInfoVO.getPremisesId()).collect(Collectors.toList());
        if(premisesIds.size()>0){
            //查询出楼盘信息并赋值
            List<PremisesInfo> premisesInfos = iPremisesInfoService.listByIds(premisesIds);
            //赋值楼盘信息
            for (int i = 0; i < premisesInfos.size() ; i++) {
                HouseInfoVO houseInfoVO = houseInfoVOList.get(i);
                houseInfoVO.setPremisesInfo(premisesInfos.get(i));
            }
        }
        //获取所有部门id
        List<Integer> deptIds = houseInfoVOList.stream().map(houseInfoVO -> houseInfoVO.getDeptId()).collect(Collectors.toList());
        if(deptIds.size()>0){
            //查询出所有的部门信息并且赋值
            List<Dept> depts = iDeptService.listByIds(deptIds);
            //赋值部门信息
            for (int i = 0; i < depts.size() ; i++) {
                HouseInfoVO houseInfoVO = houseInfoVOList.get(i);
                houseInfoVO.setDept(depts.get(i));
            }
        }
        houseInfoVOList.forEach(houseInfoVO -> {
            //赋值房产信息
            setFcmessage(houseInfoVO);
            //赋值x室x厅
            setShiting(houseInfoVO);
            //设置租房类型
            setrentType(houseInfoVO);
        });

        TableDataInfo dataTable = getDataTable(houseInfoVOList);
        int size = houseInfoVOList.size();
        dataTable.setTotal(Long.parseLong(size+""));
        return dataTable;
    }

    public void setShiting(HouseInfoVO houseInfoVO){
        Integer room = houseInfoVO.getRoom();
        Integer hall = houseInfoVO.getHall();
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder fc = stringBuilder.append((room == null ? 0 : room) + "室" + (hall == null ? 0 : hall) + "厅");
        houseInfoVO.setShiting(fc.toString());
    }

    public void setFcmessage(HouseInfoVO houseInfoVO){
        if(houseInfoVO.getPremisesInfo()==null){
            return;
        }
        if(houseInfoVO.getBuildingNo()!=null && houseInfoVO.getUnitNo()!=null && houseInfoVO.getRoomNo()!=null){
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder fc = stringBuilder.append(houseInfoVO.getBuildingNo() + "栋" + houseInfoVO.getUnitNo() + "单元" + houseInfoVO.getRoomNo());
            houseInfoVO.setFcMessage(houseInfoVO.getPremisesInfo().getName()+"("+fc.toString()+")");
            return;
        }
        houseInfoVO.setFcMessage(houseInfoVO.getPremisesInfo().getName());
    }

    public void setrentType(HouseInfoVO houseInfoVO){
        if(houseInfoVO.getType()==1){
            houseInfoVO.setZflx("公寓");
            houseInfoVO.setZflxNumber(3);
            return;
        }
        if(houseInfoVO.getReserve1()==1){
            houseInfoVO.setZflx("合租");
            houseInfoVO.setZflxNumber(1);
            return;
        }
        houseInfoVO.setZflx("整租");
        houseInfoVO.setZflxNumber(2);
    }

}
